package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/8/9 10:31
 * Description: 包信息
 */
@Component
public class SecurityIntegrationConfig extends IntegrationConfig<Integer, List<DeviceProjSupplierCategory>> {

    private Map<Integer, List<DeviceProjSupplierCategory>> SECURITY_CONFIG_MAP = new HashMap<>();

    @Override
    public List<DeviceProjSupplierCategory> getIntegrationConfig(Integer projectId) {
        return SECURITY_CONFIG_MAP.get(projectId);
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handleVehicleIntegrationConfig();
    }

    public List<DeviceProjSupplierCategory> getIntegrationConfigs() {
        List<DeviceProjSupplierCategory> list = new ArrayList<>();
        for (List<DeviceProjSupplierCategory> value : SECURITY_CONFIG_MAP.values()) {
            list.addAll(value);
        }
        return list;
    }

    private void handleVehicleIntegrationConfig() {
        SECURITY_CONFIG_MAP = IntegrationConfig.integrationConfigList.stream()
                .filter(category -> CategoryEnum.PATROLLING_EQUIPMENT.getId().equals(category.getProdCategoryId()))
                .collect(Collectors.groupingByConcurrent(DeviceProjSupplierCategory::getProjectId));
    }

}
